package com.neuedu.studentmanage.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.neuedu.studentmanage.entity.Student;
import com.neuedu.studentmanage.entity.Teacher;
import com.neuedu.studentmanage.entity.User;
import com.neuedu.studentmanage.intercepter.Const;
import com.neuedu.studentmanage.service.StudentService;
import com.neuedu.studentmanage.service.TeacherService;
import com.neuedu.studentmanage.service.UserService;

@Controller
@SessionAttributes(value = { Const.SESSION_USER })
public class LoginController {
	private Logger logger = Logger.getLogger(LoginController.class);

	@Autowired
	private TeacherService teacherService;

	@Autowired
	private StudentService studentService;

	@Autowired
	private UserService userService;
	
	@RequestMapping("/")
	public String login() {
		return "login";
	}
	
	@RequestMapping("/login")
	public String login1() {
		return "login";
	}

	// ++++++++++++++学生登录+++++++++++++++++++++++++
	@RequestMapping("/student/login")
	public String studentLogin() {

		return "studentlogin";
	}

	@PostMapping("/studentlogin")
	public String doStudentLogin(Integer userName, String userPass, Model model) {
		Student user = new Student(userName, userPass);
		Student user2 = studentService.login(user);
		if (user2 == null) {
			return "redirect:/studentlogin";
		}
		model.addAttribute(Const.SESSION_USER, user2.getStuId());
		logger.info("登陆成功，返回首页面！");
		return "redirect:/student";
	}

	// ++++++++++++++老师登录+++++++++++++++++++++++++++++
	@RequestMapping("/teacher/login")
	public String teacherLogin() {
		return "teacherlogin";
	}

	@PostMapping("/teacherlogin")
	public String doTeacherLogin(String userName, String userPass, Model model) {

		boolean flag = userName.matches("^\\d*");
		Teacher teacher = null;
		if (flag) {
			teacher = new Teacher(userName, userPass, null);
		} else {
			teacher = new Teacher(null, userPass, userName);
		}
		Teacher teacher2 = teacherService.login(teacher);

		if (teacher2 == null) {
			logger.info("登陆失败，返回教师登录页面！");
			return "redirect:/teacher/login";
		}
		model.addAttribute(Const.SESSION_USER, teacher2.getName());
		logger.info("登陆成功，返回首页面！");
		return "redirect:/teacher";
	}

	// ++++++++++++++管理员登录++++++++++++++++++++++++++
	@RequestMapping("/admin/login")
	public String adminLogin() {
		logger.info("--------------adminlogin跳转--------------");
		return "adminlogin";
	}

	@RequestMapping("/adminlogin")
	public String doAdminLogin(String userName, String userPass, Model model) {
		User user = new User(userName, userPass);
		User user2 = userService.login(user);
		if (user2 == null) {
			logger.info("登陆失败，返回管理员登录页面！");
			return "redirect:/admin/login";
		}
		model.addAttribute(Const.SESSION_USER, user2.getAccount());
		logger.info("登陆成功，返回管理员首面！");
		return "redirect:/admin";
	}

}
